#include "panel.h"
#include "ui_panel.h"
#include <QPainter>
#include <QtMath>
#include <QDebug>

Panel::Panel(QString useMem,QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Panel)
{
    ui->setupUi(this);
    setFixedSize(536, 321);
    this->useMem = useMem;
    // num = 60;
    num = useMem.mid(0,useMem.length() - 1).toInt();
    timer = new QTimer(this);
    // currentValue = 50;
    // mark = 0;
    StartAngle = 150;

    // connect(timer, &QTimer::timeout,[=](){
    //     currentValue += (mark == 0) ? 1 : -1;
    //     if (currentValue >= 60) mark = 1;
    //     if (currentValue == 0)  mark = 0;

    //     update();
    // });
    // timer->start(50);
}

Panel::~Panel()
{
    delete ui;
}

// 初始化画布 让背景为黑色
void Panel::initCanvas(QPainter &painter)
{
    // 设置抗锯齿渲染
    painter.setRenderHint(QPainter::Antialiasing,true);

    // 设置画刷为黑色，绘制整个窗口的矩形
    // painter.setBrush(Qt::black);
    painter.drawRect(rect());

    // 将坐标原点移动到窗口中心向下一点
    painter.translate(rect().width()/2, rect().height()*0.6);
}

// 画中间的小圆
void Panel::drawMiddleCircle(QPainter &painter, int radius)
{
    // 画小圆
    painter.setBrush(Qt::NoBrush);
    painter.setPen(QPen(Qt::white, 3));
    painter.drawEllipse(QPoint(0,0), radius, radius);
}

//画出当前的速度
void Panel::drawCurrentSpeed(QPainter &painter)
{
    // 设置字体样式
    QFont font("Arial", 20);// 创建字体对象，Arial 字体，大小 30
    font.setBold(true);// 设置字体为粗体
    painter.setFont(font);// 在绘图设备上设置字体
    painter.setPen(Qt::white); // 设置画笔颜色为白色
    painter.drawText(QRect(-60, -60, 120, 70), Qt::AlignCenter, useMem);// 在指定矩形区域内绘制文本，居中显示当前速度值

    QFont _font("Arial", 15);
    _font.setBold(true);
    painter.setFont(_font);
    painter.setPen(Qt::white);
    painter.drawText(QRect(-60, -60, 120, 145), Qt::AlignCenter, "内存消耗");
}

//画刻度
void Panel::drawScale(QPainter &painter, int radius)
{
    //计算角度
    Angle = 240*1.0 / 60;

    painter.setPen(QPen(Qt::black, 4));
    painter.save();
    painter.rotate(StartAngle);
    //绘制刻度
    for(int i = 0; i<=60; i++){
        if(i >= 40){
            painter.setPen(QPen(Qt::red,4));
        }
        if(i % 5 == 0){
            painter.drawLine(radius - 20, 0, radius - 2, 0);//画长刻度线
        }else{
            painter.drawLine(radius - 8, 0,  radius - 2, 0);//画短刻度线
        }
        painter.rotate(Angle);//旋转坐标
    }
    painter.restore();
    painter.setPen(Qt::black);

}

//画刻度的文字
void Panel::drawScaleText(QPainter &painter, int radius)
{
    QFont font("Arial", 15);
    painter.save();
    font.setBold(true);
    painter.setFont(font);
    int r = radius - 42;
    for(int i = 0; i <= 60; i++){
        if(i % 5 == 0){
            //保存坐标
            painter.save();
            //确定x,y坐标
            int delx = qCos(qDegreesToRadians(210 - (Angle * i))) * r;
            int dely = qSin(qDegreesToRadians(210 - (Angle * i))) * r;
            //将坐标系移动到x,y坐标上
            painter.translate(QPoint(delx, -dely));
            //旋转一下
            painter.rotate(-120 + Angle*i);//Angle=4 30*4=120 实参为0
            //写上文字
            painter.drawText(-20, -20, 40, 40, Qt::AlignCenter, QString::number(i*4));
            //回复坐标
            painter.restore();
        }
    }
}

//画指针
void Panel::drawPointLen(QPainter &painter, int len)
{

    painter.restore();
    painter.save();//保存当前的坐标点 指针指向3点钟方向

    painter.setBrush(Qt::black);
    painter.setPen(Qt::NoPen);
    static const QPointF points[4] = {//设置指针形状的坐标
        QPointF(0.0, 0.0),
        QPointF(100.0, -1.1),
        QPointF(100.0, 1.1),
        QPointF(0.0, 15.0)
    };

    painter.rotate(StartAngle + num);//指针初始化位置
    painter.drawPolygon(points, 4);
    //painter.drawLine(60, 0, len, 0);
    painter.restore();//恢复之前保存的状态
}

//画扇形
void Panel::drawSpeedPie(QPainter &painter, int radius)
{
    //painter.restore();
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(255, 0, 0, 100));
    painter.drawPie(QRect(-radius, -radius , radius*2, radius*2), (-StartAngle)*16, -num*16);//Angle加符号让指针顺时针画
}

//在中间画一个黑色的圆
void Panel::drawCircleBlack(QPainter &painter, int radius)
{
    painter.setBrush(Qt::black);
    painter.drawEllipse(QPoint(0, 0),radius,radius);
}

//在中间画一个渐变的红色
void Panel::drawCircleRed(QPainter &painter, int radius)
{
    QRadialGradient radialGradient(0, 0, 105);
    // 设置颜色停靠点
    radialGradient.setColorAt(0.0, QColor(255,0,0,200)); // 中心颜色
    radialGradient.setColorAt(1.0, QColor(0,0,0,100)); // 外围颜色
    painter.setBrush(QBrush(radialGradient));
    painter.drawEllipse(QPoint(0, 0), radius, radius);
}

//最外圈渐变色
void Panel::drawOutCircle(QPainter &painter, int radius)
{
    //painter.restore();
    painter.setPen(Qt::NoPen);// 设置画笔为无边框
    QRadialGradient radialGradient(0, 0, radius);// 创建一个径向渐变，中心在 (0, 0)，半径为 radius
    // 设置径向渐变的颜色停止点和颜色
    // 完全不透明的红色，alpha 值为 200
    radialGradient.setColorAt(1.0, QColor(255,0,0,200));
    radialGradient.setColorAt(0.97, QColor(255,0,0,120));// 略微透明的红色，alpha 值为 120
    radialGradient.setColorAt(0.9, QColor(0,0,0,0));// 完全透明的黑色
    radialGradient.setColorAt(0, QColor(0,0,0,0));// 完全透明的黑色
    painter.setBrush(QBrush(radialGradient));// 设置画刷为径向渐变
    painter.drawPie(QRect(-radius, -radius , radius*2, radius*2), -150*16, -Angle*60*16);//Angle加符号让指针顺时针画
}

//画一个宝马logo
void Panel::drawLogo(QPainter &painter, int radius)
{
    painter.drawPixmap(QRect(-45, radius*0.3, 100, 95), QPixmap(":/baoma.png"));//在一个矩形里画一个logo
}

void Panel::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    //初始化画布 让背景为黑色
    initCanvas(painter);
    //画中间的小圆
    drawMiddleCircle(painter, 60);
    //画刻度
    drawScale(painter, height()/2);
    //画刻度的文字
    drawScaleText(painter, height()/2);
    //画指针
    drawPointLen(painter, height()/2 - 48);
    //画扇形
    drawSpeedPie(painter, height()/2 + 25);
    //在中间画一个渐变的红色
    // drawCircleRed(painter, 110);
    //在中间画一个黑色的圆圈
    drawCircleBlack(painter, 50);
    //画出当前的速度
    drawCurrentSpeed(painter);
    //最外圈渐变色
    drawOutCircle(painter, height()/2 + 25);
    //画一个宝马logo
    // drawLogo(painter, height()/2);
}
